// CinemaSystem.cpp
#include "cinemasystem.h"

// Definitions for CinemaSystem
CinemaSystem::CinemaSystem(const std::string& date, const std::string& name, double price)
{}

void CinemaSystem::addAuditorium(const Auditorium& hall) {

    halls.push_back(hall);
}


void CinemaSystem::sale(int sit_info, int Auditorium_info) {
    if (sit_info < 0 || sit_info >= 24) {
        std::cerr << "Invalid seat information!" << std::endl;
        return;
    }
    halls[Auditorium_info].seats[sit_info].status = SeatStatus::SOLD;
}

// 更多成员函数的定义...

// Definitions for Seat  用于存储一个放映厅中一个座位的信息，包括行号，列号，座位的id，是否被预定

CinemaSystem::Seat::Seat(int row, int col) :
        rowNumber(row),
        columnNumber(col),
        seatID(char('A' + row) + std::to_string(col + 1)),
        status(SeatStatus::AVAILABLE)

{}
CinemaSystem::Seat::Seat() :
        rowNumber(0), // 默认值
        columnNumber(0), // 默认值
        seatID("A0"), // 一个默认值
        status(SeatStatus::AVAILABLE)

{}

CinemaSystem::Seat CinemaSystem::readseat(int seat_info,int aud_info){
    std::cout << "read successfully"<<std::endl;
    return halls[aud_info-1].seats[seat_info];
}
// Definitions for Auditorium
CinemaSystem::Auditorium::Auditorium(int numSeats,int number)
{
    hallNumber = number;
    seats.resize(numSeats);
    for (int i = 0; i < numSeats; i++) {
        seats[i] = Seat(i / 6, i % 6);  // 在向量 seats 中的每个位置创建一个 Seat 对象
    }
}


 std::string CinemaSystem::readDateFromScreen() {
    std::regex date_pattern("^\\d{4}-\\d{2}-\\d{2}$");  // YYYY-MM-DD
    std::string input;

    while (true) {
        std::cout << "Enter a date in the format YYYY-MM-DD: ";
        std::cin >> input;

        if (std::regex_match(input, date_pattern)) {
            // 进一步验证月份和日期的有效性
            int year = std::stoi(input.substr(0, 4));
            int month = std::stoi(input.substr(5, 2));
            int day = std::stoi(input.substr(8, 2));

            if (month >= 1 && month <= 12) {
                int maxDays = 31;  // Most months have 31 days
                if (month == 2) {
                    // February: check for leap year
                    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                        maxDays = 29;
                    } else {
                        maxDays = 28;
                    }
                } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                    maxDays = 30;  // Months with 30 days
                }

                if (day >= 1 && day <= maxDays) {
                    return input;  // Valid date
                }
            }
        }

        std::cout << "Invalid date format. Please try again." << std::endl;
    }
}

void CinemaSystem::saveAuditoriumsToFile(const std::string& filename) {
    std::ofstream outFile(filename);
    if (!outFile) {
        std::cerr << "Error opening file for writing: " << filename << std::endl;
        return;
    }

    for (const auto& aud : halls) {
        outFile << aud.hallNumber << "\n";
        for (const auto& entry : aud.movie) {
            outFile << entry.first << "," << entry.second.first << "," << entry.second.second << "\n";
            for (const auto& seat : aud.seats) {
                outFile << (seat.status == CinemaSystem::SeatStatus::AVAILABLE ? '0' : '1');
            }
            outFile << "\n";  // 新的行，表示座位信息结束
        }
        outFile << "END_AUD\n";  // 标记放映厅数据的结束
    }

    outFile.close();
}

std::vector<CinemaSystem::Auditorium> CinemaSystem::readAuditoriumsFromFile(const std::string& filename) {
    std::ifstream inFile(filename);
    if (!inFile) {
        std::cerr << "Error opening file for reading: " << filename << std::endl;
        return {};
    }

    // 打开 D:\\QT_projects\\Login\\4.txt 并读取其内容
    std::ifstream replaceFile("D:\\QT_projects\\Login\\4.txt");
    if (!replaceFile) {
        std::cerr << "Error opening replacement file for reading." << std::endl;
        return {};
    }
    std::vector<std::string> replaceStrings;
    std::string replaceLine;
    while (std::getline(replaceFile, replaceLine)) {
        replaceStrings.push_back(replaceLine);
    }
    replaceFile.close();

    std::vector<CinemaSystem::Auditorium> auditoriums;
    std::string line;
    int replaceIndex = 0;

    while (std::getline(inFile, line)) {
        std::cout << "Line: " << line << std::endl;
        int hallNumber = std::stoi(line);
        std::map<std::string, std::pair<std::string, double>> movieMap;
        std::vector<CinemaSystem::Seat> allSeats;

        while (std::getline(inFile, line) && line != "END_AUD") {
            std::stringstream ss(line);
            std::string date, movieName, ticketPriceStr;
            std::getline(ss, date, ',');
            if (ss.good()) {
                std::getline(ss, movieName, ',');
                std::getline(ss, ticketPriceStr, ',');
                double ticketPrice = std::stod(ticketPriceStr);
                movieMap[date] = {movieName, ticketPrice};
            } else {
                // 替换逻辑
                if (replaceIndex < replaceStrings.size()) {
                    line = replaceStrings[replaceIndex++];
                }

                // This is the seat information
                for (char ch : line) {
                    CinemaSystem::Seat seat;
                    seat.status = (ch == '0') ? CinemaSystem::SeatStatus::AVAILABLE : CinemaSystem::SeatStatus::SOLD;
                    allSeats.push_back(seat);
                }
            }
        }

        CinemaSystem::Auditorium aud(24, 0);  // Placeholder values
        aud.hallNumber = hallNumber;
        aud.movie = movieMap;
        aud.seats = allSeats;
        auditoriums.push_back(aud);
    }

    inFile.close();
    return auditoriums;
}

void d1elete(const std::string& filePath) {
    // 读取文件内容
    std::ifstream inputFile(filePath);
    std::vector<std::string> lines;
    std::string line;
    while (std::getline(inputFile, line)) {
        lines.push_back(line);
    }
    inputFile.close();

    // 删除最后四行
    size_t numLines = lines.size();
    if (numLines <= 4) {
        lines.clear();
    } else {
        lines.erase(lines.end() - 4, lines.end());
    }

    // 将修改后的内容写回文件
    std::ofstream outputFile(filePath);
    for (const std::string& line : lines) {
        outputFile << line << std::endl;
    }
    outputFile.close();
}
